package com.lrh.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.LongPredicate;
import java.util.function.Predicate;

public class LambdaUsage {

    private static List<Apple> filter(List<Apple> source, Predicate<Apple> predicate){
        List<Apple> result = new ArrayList<Apple>();
        for (Apple apple : source) {
            if(predicate.test(apple))
                result.add(apple);
        }
        return result;
    }

    private static List<Apple> filterByWeight(List<Apple> source, LongPredicate predicate){
        List<Apple> result = new ArrayList<Apple>();
        for (Apple apple : source) {
            if(predicate.test(apple.getWeigth()))
                result.add(apple);
        }
        return result;
    }

    public static void main(String[] args) {
        /*Runnable r1 = () -> System.out.println("Hello");

        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello2");
            }
        };

        process(r1);
        process(r2);
        process(() -> System.out.println("Hello3"));

        */

        List<Apple> list  = Arrays.asList(new Apple("green", 120), new Apple("red", 150));
        List<Apple> result = filter(list, Apple -> Apple.getWeigth() > 130);
        System.out.println(result);

        List<Apple> result2 = filterByWeight(list, weight -> weight < 130);
        System.out.println(result2);
    }

    private static void process(Runnable r){
        r.run();
    }
}
